#!/bin/bash
#global.functions

#tools
export SERVER_IP="182.92.106.104"
#webs can use svn,ftp;
#apps can't use anyone,only -.
export DataHome="/data/PaaSdp"
export INIT_HOME="${DataHome}/USERS"
export uidfile=${INIT_HOME}/.uid
export Sdpuc=${INIT_HOME}/.Ucenter
export webs=("nginx" "httpd" "tomcat")
export apps=("mongodb" "memcached" "redis" "mysql")
export files=("svn" "ftp" "-")
export services=("mongodb" "memcached" "redis" "mysql" "nginx" "httpd" "tomcat")
export svnconf="/etc/httpd/conf.d/subversion.conf"
export httpasswd="${INIT_HOME}/.httpasswd"
export vfu="/etc/vsftpd/vfu.list"
export vfudb="/etc/vsftpd/vfu.db"
export vfudir="/etc/vsftpd/vfu_dir"
export uidfile="${INIT_HOME}/.uid"
export LogDir="${DataHome}/logs"
export Suclog="${LogDir}/sdp.access.log"
export Errlog="${LogDir}/sdp.error.log"
export PreciseTime=$(date +"%Y-%m-%d_%H:%M:%S")


function ERROR() {
  echo "Error:Please check this script and input/output!" >&2
  [ -d $init_user_home ] && rm -rf $init_user_home ;
  [ -f $user_nginx_conf ] && rm -f $user_nginx_conf ;
  if echo "${webs[@]}" | grep -w $init_service_type &> /dev/null ;then
	[ -f $dnmap_file ] && sed -i "/$init_user_dns/d" $dnmap_file
  fi
  if [ "$init_file_type" = "svn" ]; then
    sed -i "/<Location \/sdi\/$init_user>/,\$d" $svnconf
  elif [ "$init_file_type" = "ftp" ]; then
    sed -i "/$init_user/,+1d" $vfu
    db_load -T -t hash -f $vfu $vfudb ; /etc/init.d/vsftpd reload
  fi
  if [ -e $uidfile ];then
    if [ `cat $uidfile` -eq 1 ]; then
      rm -f $uidfile
    else
      echo $((`cat $uidfile` - 1)) > $uidfile
    fi
  fi
cat >> $Errlog <<EOF
${PreciseTime} ${init_user} ${user_email} ErrAction:rm -rf ${init_user_home}, rm -f ${user_nginx_conf}, del $uidfile
EOF
  exit 1;
}

function dockererror() {
  local init_user_container=`docker ps -a | grep $init_user | wc -l`
  if [ $init_user_container -gt 0 ]; then
    :
  else
    docker rm -f $container_id
  fi
cat >> $Errlog <<EOF
${PreciseTime} ${init_user} ${user_email} ErrAction:docker rm -f ${container_id}
EOF
}

function DoubleError() {
  ERROR
  dockererror
}

function create_ftp() {
[ "$#" != "3" ] && ERROR
#arg:$init_user $init_passwd $init_user_home_root
cat >> $vfu <<EOF
$1
$2
EOF
db_load -T -t hash -f $vfu $vfudb
cat > ${vfudir}/$1 <<EOF
write_enable=YES
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_root=$3
EOF
chown -R ftp.ftp $3
chmod -R a+t $3
/etc/init.d/vsftpd reload
}


function create_svn() 
{
#arg:$init_user $init_passwd
init_user_home_svnroot=${init_user_home}/$init_user
svnadmin create $init_user_home_svnroot ; chown -R apache:apache $init_user_home_svnroot
[ "$#" != "2" ] && ERROR
cat >> $svnconf <<EOF

<Location /sdi/$1>
   DAV svn
   SVNPath $init_user_home_svnroot
   AuthType Basic
   AuthName "Welcome to Sdp CodeSourceRoot."
   AuthUserFile $httpasswd
   #SSLRequireSSL
  <LimitExcept GET PROPFIND OPTIONS REPORT>
    Require valid-user
  </LimitExcept>
</Location>
EOF
[ -e $httpasswd ] && htpasswd -mb $httpasswd $1 $2 || htpasswd -bc $httpasswd $1 $2
/etc/init.d/httpd reload
}

function AutoUpdateSvn() {
cd $init_user_home ; svn co https://svn.saintic.com/sdi/$init_user root;
cd ${init_user_home_svnroot}/hooks/;
cat > post-commit <<EOF
#!/bin/bash
export LC_CTYPE=en_US.UTF-8
export LANG=en_US.UTF-8
cd $init_user_home_root ;  svn up
EOF
chmod -R 777 post-commit
chown -R apache.apache post-commit
chmod -R 777 $init_user_home_root
}

function MD5PASSWD() {
  cat /dev/urandom | tr -dc "a-zA-Z0-9_+\~\!\@\#\$\%\^\&\*\(\)"| fold -w 12 |head -n 1 | md5sum  |  awk  '{print $1}' | fold -w 12 | head -1
}

